// pages/userManage/userManage.js
Page({
  
  /**
   * 页面的初始数据
   */
  data: {
    noManager:false,
    noConnect:false,
    windowOn:false,
    now_list:[],
    total_list:[],
    window_info:[],
    is_manager:0,
    nickname:"",
    score:0,
    correct:0,
    incorrect:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    var openid = wx.getStorageSync('userinfo').openid
    wx.request({
      url:getApp().globalData.url+"getuserlist/",
      data:{
          data:"",
          openid:openid
      },
      header:{
        "Content-Type":"application/json"
      },
      success:function(res){
        if(res.data == "not manager"){
          that.setData({
            now_list:[],
            total_list:[],
            noManager:true,
            windowOn:false,
          
        })
        }else{
          that.setData({
            now_list:res.data,
            total_list:res.data,
            windowOn:false,
            
          })
        }
      },
      fail:function(res){
        that.setData({
          noConnect:true
        })
      }
  })
},

  edite:function(e){
    var that = this
    var id = parseInt(e.currentTarget.id)
    var list = that.data.now_list
    for(var i=0 ;i<list.length;i++){
      if(list[i].id == id){
        that.setData({
          window_info:list[i],
          is_manager:list[i].is_manager,
          nickname:list[i].nickname,
          score:list[i].score,
          correct:list[i].correct_num,
          incorrect:list[i].incorrect_num
        })
      }
    }





    that.setData({
      windowOn:true
    })
  },



  cancel:function(e){
    var that = this
    that.setData({
      windowOn:false
    })

  },
  setManage:function(e){
    var that = this
    if(that.data.is_manager == 1){
      that.setData({
        is_manager:0
      })
    }else if (that.data.is_manager == 0){
      that.setData({
        is_manager:1
      })
    }
  },
  save:function(e){
    var that = this


    wx.showModal({

      content: '确认保存？',
      success: function (res) {
        if (res.confirm) {  
          var openid = wx.getStorageSync('userinfo').openid
          wx.request({
            url:getApp().globalData.url+"saveuser/",
            data:{
              type:"setuserinfo",
              openid:openid,
              id:that.data.window_info.id,
              is_manager:that.data.is_manager,
              nickname:that.data.nickname,
              score:that.data.score,
              correct_num:that.data.correct,
              incorrect_num:that.data.incorrect

            },
            header:{
              "Content-Type":"application/json"
            },
            
            success:function(res){
              that.onLoad()
            },
            fail:function(res){
      
            }
        })
        }
      }
    })



    
  },



  nickname:function(e){
    var that = this
    var nickname = e.detail.value
    that.setData({
      nickname:nickname
    })
    console.log(that.data.nickname)
  },
  score:function(e){
    var that = this
    var score = e.detail.value
    that.setData({
      score:score
    })
  },
  correct:function(e){
    var that = this
    var correct = e.detail.value
    that.setData({
      correct:correct
    })
  },
  incorrect:function(e){
    var that = this
    var incorrect = e.detail.value
    that.setData({
      incorrect:incorrect
    })
  },
  deleteUser:function(e){
    var that = this

    wx.showModal({

      content: '确认删除用户？',
      success: function (res) {
        if (res.confirm) {  
          var openid = wx.getStorageSync('userinfo').openid
          wx.request({
            url:getApp().globalData.url+"saveuser/",
            data:{
              type:"deleteuser",
              openid:openid,
              id:that.data.window_info.id,
            },
            header:{
              "Content-Type":"application/json"
            },
            
            success:function(res){
              that.onLoad()
            },
            fail:function(res){
      
            }
        })
        }
      }
    })
  }
})